package com.baseproject.slidingmenu;

import android.content.Context;
import android.support.v4.view.MotionEventCompat;
import android.support.v4.widget.SlidingPaneLayout;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewConfiguration;

/**
 * 自定义侧边栏布局
 * Created by hp on 2017/5/23.
 */

public class MySliddingMenu extends SlidingPaneLayout {
    private float mEdgeSlop;
    private float mInitialMotionX;
    private boolean slidingEnable = true;

    public void setSlidingMenuEnable(boolean enable) {
        slidingEnable = enable;
    }

    public MySliddingMenu(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public MySliddingMenu(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MySliddingMenu(Context context) {
        super(context);
        ViewConfiguration config = ViewConfiguration.get(context);
        mEdgeSlop = config.getScaledEdgeSlop();
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (MotionEventCompat.getActionMasked(ev)) {
            case MotionEvent.ACTION_DOWN:
                mInitialMotionX = ev.getX();
                break;
            case MotionEvent.ACTION_MOVE:
                float x = ev.getX();
                float y = ev.getY();
                if (!slidingEnable || (mInitialMotionX > mEdgeSlop && !isOpen() && canScroll(this, false, Math.round(x - mInitialMotionX),
                        Math.round(x), Math.round(y)))) {
                    MotionEvent cancelEvent = MotionEvent.obtain(ev);
                    cancelEvent.setAction(MotionEvent.ACTION_CANCEL);
                    return super.onInterceptTouchEvent(cancelEvent);
                }
        }
        return super.onInterceptTouchEvent(ev);
    }
}
